Docs Menu
Docs Home

インデックスを使用したクエリの最適化

このページでは、 Cドライバーを使用してさまざまなタイプのインデックスを管理する方法を示すコピー可能なコード例があります。

Tip

インデックスの操作の詳細については、インデックスガイドを参照してください。このページに表示されているインデックスについて詳しく学ぶには、各セクションに提供されているリンクを参照してください。

このページの例を使用するには、コード例をサンプル アプリケーションまたは独自のアプリケーションにコピーします。 <connection string URI>など、コード例にあるすべてのプレースホルダーを、MongoDB 配置に関連する値に置き換えてください。

次のサンプルアプリケーションを使用して、このページのコード例をテストできます。 サンプル アプリケーションを使用するには、次の手順を実行します。

  1. Cドライバーがインストールされていることを確認します。

  2. 次のコードをコピーし、新しい.cファイルに貼り付けます。

  3. このページからコード例をコピーし、 ファイル内の指定された行に貼り付けます。

1#include <bson/bson.h>
2#include <mongoc/mongoc.h>
3#include <stdio.h>
4
5int
6main(void)
7{
8 bson_error_t error;
9
10 mongoc_init();
11
12 mongoc_client_t *client = mongoc_client_new("<connection string URI>");
13 mongoc_collection_t *collection = mongoc_client_get_collection(client, "<database name>", "collection name");
14
15 // Start example code here
16
17 // End example code here
18
19 mongoc_collection_destroy(collection);
20 mongoc_client_destroy(client);
21 mongoc_cleanup();
22
23 return EXIT_SUCCESS;
24}

次の例では、指定されたフィールドに昇順のインデックスを作成しています。

bson_t *keys = BCON_NEW("<field name>", BCON_INT32(1));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

単一フィールド インデックスの詳細については、「単一フィールド インデックス」のガイドを参照してください。

次の例では、指定されたフィールドに対して 2 つの昇順インデックスからなる複合インデックスを作成します。

bson_t *keys = BCON_NEW("<field name 1>", BCON_INT32(1), "<field name 2>", BCON_INT32(1));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

複合インデックスの詳細については、「複合インデックス」のガイドを参照してください。

次の例では、指定された配列値フィールドに昇順のマルチキーインデックスを作成しています。

bson_t *keys = BCON_NEW("<array field name>", BCON_INT32(1));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

マルチキー インデックスの詳細については、「 マルチキーインデックス」のガイドを参照してください。

次の例では、GeoJSON オブジェクトを含む指定されたフィールドに2 dsphere インデックスを作成します。

bson_t *keys = BCON_NEW("<GeoJSON object field name>", BCON_UTF8("2dsphere"));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

GeoJSONデータ型の詳細については、 MongoDB Serverマニュアルの 「 GeoJSON オブジェクト」 を参照してください。

次の例では、指定されたフィールドに昇順のユニークインデックスを作成しています。

bson_t *keys = BCON_NEW("title", BCON_INT32(1));
bson_t *opts = BCON_NEW("unique", BCON_BOOL(true));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, opts);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
bson_destroy(opts);
mongoc_index_model_destroy(index_model);

次の例では、指定されたコレクションに昇順のワイルドカードを作成しています。

bson_t *keys = BCON_NEW("$**", BCON_INT32(1));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

次の例では、 _idフィールドに昇順のクラスターインデックスを含む新しいコレクションを作成します。

bson_t *opts = BCON_NEW("clusteredIndex", "{",
"key", "{",
"_id", BCON_INT32(1),
"}",
"unique", BCON_BOOL(true),
"}");
mongoc_database_t *database = mongoc_client_get_database(client, "<database name>");
if (mongoc_database_create_collection(database, "<collection name>", opts, &error)) {
printf("Successfully created collection\n");
} else {
fprintf(stderr, "Failed to create collection: %s", error.message);
}
mongoc_database_destroy(database);
bson_destroy(opts);

次の例では、指定された string フィールドにテキスト インデックスを作成します。

bson_t *keys = BCON_NEW("<field name>", BCON_UTF8("text"));
mongoc_index_model_t *index_model = mongoc_index_model_new(keys, NULL);
if (mongoc_collection_create_indexes_with_opts(collection, &index_model, 1, NULL, NULL, &error)) {
printf("Successfully created index\n");
} else {
fprintf(stderr, "Failed to create index: %s", error.message);
}
bson_destroy(keys);
mongoc_index_model_destroy(index_model);

次の例では、指定された名前のインデックスを 1 つ削除します。

if (mongoc_collection_drop_index(collection, "<index name>", &error)) {
printf("Successfully dropped index\n");
} else {
fprintf(stderr, "Failed to drop index: %s", error.message);
}

インデックスの削除の詳細については、「 インデックスとの連携 」ガイドの「 インデックスの削除 」を参照してください。

次のセクションには、MongoDB 検索インデックスを管理する方法を説明するコード例が含まれています。

MongoDB 検索インデックスについて詳しく学ぶには、MongoDB 検索インデックス ガイドを参照してください。

次の例では、指定されたフィールドにMongoDB検索インデックスを作成します。

bson_t cmd;
char *cmd_str = bson_strdup_printf(
BSON_STR({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ]
}),
"<collection name>");
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully created search index\n");
} else {
fprintf(stderr, "Failed to create search index: %s", error.message);
}
bson_destroy(&cmd);

検索インデックスの作成の詳細については、「 検索インデックスの作成 」ガイドを参照してください。

次の例では、指定されたコレクション内のMongoDB 検索インデックスのリストを出力します。

bson_t pipeline;
const bson_t *doc;
const char *pipeline_str = BSON_STR({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_init_from_json(&pipeline, pipeline_str, -1, &error);
mongoc_cursor_t *cursor =
mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)) {
char *str = bson_as_canonical_extended_json(doc, NULL);
printf("%s\n", str);
bson_free(str);
}
bson_destroy(&pipeline);
mongoc_cursor_destroy(cursor);

検索インデックスをリストする方法について詳しくは、「 検索インデックスをリストする 」のガイドを参照してください。

次の例では、指定された新しいインデックス定義で既存のMongoDB検索インデックスを更新します。

bson_t cmd;
char *cmd_str = bson_strdup_printf(
BSON_STR({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}),
"<collection name>");
bson_init_from_json(&cmd, cmd_str, -1, &error);
bson_free(cmd_str);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully updated search index\n");
} else {
fprintf(stderr, "Failed to create search index: %s", error.message);
}
bson_destroy(&cmd);

検索インデックスの更新について詳しくは、「 検索インデックスを更新する 」ガイドを参照してください。

次の例では、指定された名前のMongoDB検索インデックスを削除します。

bson_t cmd;
char *cmd_str = bson_strdup_printf(
BSON_STR({
"dropSearchIndexes" : "%s",
"index" : "<index name>"
}),
"<collection name>");
bson_init_from_json(&cmd, cmd_str, -1, &error);
if (mongoc_collection_command_simple(collection, &cmd, NULL, NULL, &error)) {
printf("Successfully deleted search index\n");
} else {
fprintf(stderr, "Failed to delete search index: %s", error.message);
}
bson_destroy(&cmd);

検索インデックスの削除の詳細については、「 検索インデックスの削除 」ガイドを参照してください。